//
//  YTFHomeTableViewCell.swift
//  sinaWeibo
//
//  Created by pro on 16/3/1.
//  Copyright © 2016年 pro. All rights reserved.
//

import UIKit
import SnapKit

class YTFHomeTableViewCell: UITableViewCell {
    
    //定义一个属性来记录底部视图的顶部约束
    var topConstraint: Constraint?

    var statusViewModel: YTFStatusViewModel?{
        didSet{
            originalView.statusViewModel = statusViewModel
            footerView.footerViewModel = statusViewModel
            
            //清空之前的约束
            topConstraint?.uninstall()
            //判断是否有转发微博
            if statusViewModel?.statusModel?.retweeted_status == nil {
                
                //没有转发微博的情况
                //隐藏转发视图
                repostView.hidden = true
                
                //修改记录的约束
                footerView.snp_updateConstraints(closure: { (make) -> Void in
                    self.topConstraint = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }else{
            
                //有转发微博的情况
                //显示转发视图
                repostView.hidden = false
                
                //赋值
                repostView.repostViewModel = statusViewModel
                
                //修改约束
                footerView.snp_updateConstraints(closure: { (make) -> Void in
                    self.topConstraint = make.top.equalTo(repostView.snp_bottom).constraint
                })
            }
            
        }
    }
    
    //重写构造方法
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //添加控件并设置约束
    private func setupUI(){
    
        //添加控件,控件是添加在contentView中的
        contentView.addSubview(originalView)
        contentView.addSubview(footerView)
        contentView.addSubview(repostView)
        
        //设置约束
        //原创微博
        originalView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView.snp_top)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
//            make.bottom.equalTo(footerView.snp_top)
        }
        
        //转发视图
        repostView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            
        }
        
        
        //当没有转发微博的时候，修改底部视图的顶部约束
        //底部视图
        footerView.snp_makeConstraints { (make) -> Void in
            
            //把这个约束赋值给成员属性
            self.topConstraint = make.top.equalTo(repostView.snp_bottom).constraint
            
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.bottom.equalTo(contentView.snp_bottom)
            
            //设置高度
            make.height.equalTo(40)
        }
        
    }
    
    //懒加载
    //原创微博
    private lazy var originalView: YTFOriginalView = YTFOriginalView()
    
    //底部视图
    private lazy var footerView: YTFFooterView = YTFFooterView()
    
    //转发视图
    private lazy var repostView: YTFRepostView = YTFRepostView()
    
    
}
